// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
/**
 * 升级
 */
let base = require("panelBase")
let viewConfig = {
    btn_close:{
        node:null,
        name:"btn_close",
        eventType:"click",
        cb:"onBtnBackClick"
    },
    //显示分红蛋糕
    fh_cake:{
        node:null,
        name:"fhCake",
        active:false,
    },
    updateSucess:{
        node:null,
        name:"updateSucess",
        active:false,
    },
    updateFree:{
        node:null,
        name:"updateFree",
        active:false,
    },

    //分红蛋糕相关
    effect_des:{
        node:null,
        name:"fhCake/effect_des",
    },
    effect:{
        node:null,
        name:"fhCake/effect"
    },
    cake:{
        node:null,
        name:"fhCake/cake"
    },
    cdTime:{
        node:null,
        name:"fhCake/time_bg/time"
    },
    btn_confirm:{
        node:null,
        name:"fhCake/btn_confirm",
        eventType:"click",
        cb:"onBtnConfirmClick"
    },
    btn_right:{
        node:null,
        name:"fhCake/right",
        eventType:"click",
        cb:"onBtnRightClick"
    },

    //update sucess 相关
    // 等级信息
    level:{
        node:null,
        name:"updateSucess/level_info",

    },
    //升级信息
    update_info:{
        node:null,
        name:"updateSucess/fh_info"
    },
    //分红收益
    fh_gain:{
        node:null,
        name:"updateSucess/fh_gain"
    },
    upcake:{
        node:null,
        name:"updateSucess/cake"
    },
    upEffect:{
        node:null,
        name:"updateSucess/effect"
    },
    //奖励
    reward:{
        node:null,
        name:"updateSucess/reward"
    },

    btn_banner:{
        node:null,
        name:"updateSucess/btn_banner",
        eventType:"click",
        cb:"onBtnBannerClick"
    },

    ///update free 相关
    f_level:{
        node:null,
        name:"updateFree/level",
    },
    //节省多少福气值
    save:{
        node:null,
        name:"updateFree/save_coin",
    },
    f_btn_banner:{
        node:null,
        name:"updateFree/btn_banner",
        eventType:"click",
        cb:"onBtnFreeBannerClick"
    },
    //低级蛋糕
    cake_l:{
        node:null,
        name:"updateFree/cake_l",
    },
    //高级蛋糕
    cake_h:{
        node:null,
        name:'updateFree/cake_h'
    }
};
cc.Class({
    extends: base,

    properties: {
        myCountDown:null,
    },

    init(){
        this._super(viewConfig);
        this.node.addComponent("myCountDown")
        this.myCountDown = this.node.getComponent("myCountDown")
        this.myCountDown.init(viewConfig["cdTime"].node);
        this.isInited = true;
        this.onHide();
    },

    onShow(){
        this._super();

        // 测试代码
        // setTimeout(()=>{
        //     /*
        //     let fhData = {countTime:150};
        //     this.onShowFhCake(fhData);
        //     */
        //    /*
        //    let upData = {
        //        tLevel:38,
        //        curLevel:6,
        //        tName:"婉约盛开",
        //        gain:180,
        //        coin:1000,
        //        rank:57,
        //    }
        //    this.onUpdateSucess(upData)
        //    */
        //   let freeData = {
        //     curLevel:6,
        //     rName:"婉约盛开",
        //     save:300,
        //     highLevel:7,
        //     curRank:58,
        //     hightRank:56,
        //     highCakeName:"充值100万"
        //   }
        //   this.onFreeUpdate(freeData)
        // },1000)
    },

    onBtnBackClick(){
        this.onHide();
    },

    onShowFhCake(data){
        if(!data)
            return ;
        let time = data.countTime;
        let min = time / 60;
        let sec = time - min * 60;
        let str = `持续${min}分${sec}秒获得平台广告分红`
        this.updateInfo(viewConfig["effect_des"].node,str,cc.Label);
        let finishCb = ()=>{
            this.startEffect(viewConfig["effect"].node,false);
            this.onHide();
        }
        this.myCountDown.startCountDown(time,null,finishCb,1,1);
        this.startEffect(viewConfig["effect"].node);
        viewConfig["fh_cake"].node.active = true;
    },

    onUpdateSucess(data){
        if(!data)
            return ;
        let tLevel = data.tLevel;
        let curLevel = data.curLevel;
        let rName = data.rankName;
        let str = `离${tLevel}级分红蛋糕，还差${tLevel-curLevel}级`
        let gain = `今日分红蛋糕收益${data.gain}元/只`;
        let reward = `恭喜获得${data.coin}金币`
        this.updateInfo(viewConfig["level"].node,curLevel+rName,cc.Label);
        this.updateInfo(viewConfig["update_info"].node,str,cc.Label);
        this.updateInfo(viewConfig["fh_gain"].node,gain,cc.Label);
        this.updateInfo(viewConfig["reward"].node,reward,cc.Label);
        this.startEffect(viewConfig["upEffect"].node);
        let cakeList = this.getCakeList();
        let cakeFrame = cakeList.getSpriteFrame(`cake_${data.rank}`);
        this.updateInfo(viewConfig["cake"].node,cakeFrame,cc.Sprite);
        viewConfig["updateSucess"].node.active = true;
    },

    onFreeUpdate(data){
        if(!data)
            return ;
          
        let curLevel = data.curLevel;
        let rName = data.rName;
        this.updateInfo(viewConfig["f_level"].node,"到"+curLevel+rName,cc.Label);
        this.updateInfo(viewConfig["save"].node,`节省${data.save}福气值`,cc.Label);
        this.updateCake(viewConfig["cake_l"].node,{rank:data.curRank,rName:data.rName});
        this.updateCake(viewConfig["cake_l"].node,{rank:data.hightRank,rName:data.highCakeName},true);

        viewConfig["updateFree"].node.active = true;
    },

    onBtnConfirmClick(){
        cc.log("on click banner");
    },

    onBtnRightClick(){
        cc.log("on btn right click");
    },
    onBtnBannerClick(){
        cc.log("on show banner");
    },
    onBtnFreeBannerClick(){
        cc.log("update free ");
    },
    updateCake(node,data,isHigh = false){
        if(!node)
            return ;
        let level = data.level + data.rName;
        this.updateInfo(node.getChildByName("level"),level,cc.Label);
        this.updateInfo(node.getChildByName("cake"),this.getCakeByName(`cake_${data.rank}`),cc.Sprite);
        if(isHigh)
        {
            this.startEffect(node.getChildByName("effect"));
        }
    },
    startEffect(node,isStart = true){
        if(!node)
        {
            return ;
        }
        if(isStart)
        {
            let rotate = cc.rotateBy(0.1,15)
            node.runAction(cc.repeatForever(rotate));
        }
        else
        {
            node.stopAllActions();
        }
    },

    onDestroy(){
        for(let key in viewConfig)
        {
            viewConfig[key].node = null;
        }
    },


    
});
